package pe.edu.pucp.notasmoviles.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import pe.edu.pucp.notasmoviles.domain.Usuario;
import pe.edu.pucp.notasmoviles.service.UsuarioService;

// Los controller se anotan con @Controller
@Controller
public class LoginController {
	private Logger logger = Logger.getLogger(LoginController.class);

	@Autowired
	private UsuarioService usuarioService;

	// Este el HandlerMapping
	@RequestMapping("/login.htm")
	public ModelAndView login(@RequestParam("username") String username,
			@RequestParam("password") String password) throws Exception {
		logger.debug("Inicio handleRequest");
		Usuario usuario = usuarioService.login(username, password);
		return new ModelAndView("bienvenida", "usuario", usuario);
	}

	@RequestMapping("/bienvenida.htm")
	public String bienvenida(HttpServletRequest req) {
		logger.debug("ip=" + req.getRemoteAddr());
		logger.debug("name="
				+ SecurityContextHolder.getContext().getAuthentication()
						.getName());
		return "bienvenida";
	}
}